package com.hotau.product.category;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hotau.HotauActivity;
import com.hotau.R;
import com.hotau.framework.HeaderNotVisible;
import com.hotau.product.category.vo.ProductCategory;

public class ProductCategoryFragment extends Fragment implements
		HeaderNotVisible, OnItemClickListener {

	private ListView categoryList;
	private List<Map<String, Object>> categoryDataList;
	private TextView categoryTitle;
	private LinearLayout categoryLayout;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initWidgets();
		initEvent();
		initData();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		return inflater.inflate(R.layout.product_category_list, container, false);
	}

	private void initWidgets() {
		categoryList = (ListView) getActivity().findViewById(R.id.category_list_view);
		categoryTitle = (TextView) getActivity().findViewById(R.id.category_title);
		categoryLayout = (LinearLayout) getActivity().findViewById(R.id.category_list_layout);
		
		LinearLayout.LayoutParams categoryLayoutParams = (LinearLayout.LayoutParams)categoryList.getLayoutParams();
		categoryLayoutParams.height = HotauActivity.window_height - categoryTitle.getHeight();
		categoryList.setLayoutParams(categoryLayoutParams);
	}

	private void initEvent() {
		categoryList.setOnItemClickListener(this);
	}

	private void initData() {
		
		categoryTitle.setText("全部类目");
		
		categoryDataList = new ArrayList<Map<String, Object>>(0);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("category_list_item", new ProductCategory("0", "所有宝贝"));
		categoryDataList.add(dataMap);
		
		dataMap = new HashMap<String, Object>();
		dataMap.put("category_list_item", new ProductCategory("1", "餐饮"));
		categoryDataList.add(dataMap);

		dataMap = new HashMap<String, Object>();
		dataMap.put("category_list_item", new ProductCategory("2", "日常用品"));
		categoryDataList.add(dataMap);

		dataMap = new HashMap<String, Object>();
		dataMap.put("category_list_item", new ProductCategory("3", "厨具/烹饪"));
		categoryDataList.add(dataMap);

		dataMap = new HashMap<String, Object>();
		dataMap.put("category_list_item", new ProductCategory("4", "办公用品"));
		categoryDataList.add(dataMap);

		dataMap = new HashMap<String, Object>();
		dataMap.put("category_list_item", new ProductCategory("5", "果蔬"));
		categoryDataList.add(dataMap);

		dataMap = new HashMap<String, Object>();
		dataMap.put("category_list_item", new ProductCategory("6", "服饰"));
		categoryDataList.add(dataMap);

		dataMap = new HashMap<String, Object>();
		dataMap.put("category_list_item", new ProductCategory("7", "家居用品"));
		categoryDataList.add(dataMap);

		dataMap = new HashMap<String, Object>();
		dataMap.put("category_list_item", new ProductCategory("8", "鞋帽"));
		categoryDataList.add(dataMap);

		dataMap = new HashMap<String, Object>();
		dataMap.put("category_list_item", new ProductCategory("9", "数码"));
		categoryDataList.add(dataMap);

		categoryList.setAdapter(new CategoryAdapter(getActivity(),
				categoryDataList, R.layout.product_category_item,
				"category_list_item", R.id.category_item_txt));
	}

	class CategoryAdapter extends BaseAdapter {

		private List<? extends Map<String, ?>> dataList;
		private LayoutInflater mInflater;
		private String mfrom;
		private int mto;
		private int itemLayout;

		public CategoryAdapter(Context context,
				List<? extends Map<String, ?>> data, int itemLayout,
				String from, int to) {
			this.dataList = data;
			this.mfrom = from;
			this.itemLayout = itemLayout;
			this.mto = to;
			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		/**
		 * @see android.widget.Adapter#getCount()
		 */
		public int getCount() {
			return dataList.size();
		}

		/**
		 * @see android.widget.Adapter#getItem(int)
		 */
		public Object getItem(int position) {
			return dataList.get(position);
		}

		/**
		 * @see android.widget.Adapter#getItemId(int)
		 */
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View view = convertView;
			if (view == null)
				view = mInflater.inflate(itemLayout, parent, false);

			TextView textView = (TextView) view.findViewById(mto);
			ProductCategory pc = (ProductCategory)((Map) dataList.get(position)).get(mfrom);
			textView.setText(pc.getCategoryText());
			return view;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		
	}
}
